using System.Collections.Generic;

namespace VSTalk.Engine.Core.ContactInfo
{
    public class ConactDetailsProvider : IContactDetailsMonitor
    {
        private ContactDetails _latest;
        private string _key;
        private readonly HashSet<IContactDetailsMonitor> _monitors = new HashSet<IContactDetailsMonitor>();

        public ConactDetailsProvider(string key, ContactDetails latest)
        {
            _latest = latest;
            _key = key;
        }

        public bool IsEmpty()
        {
            return _monitors.Count == 0;
        }

        public void Add(IContactDetailsMonitor monitor)
        {
            if (_monitors.Contains(monitor))
            {
                return;
            }
            if (_latest != null)
            {
                monitor.Set(_latest);
            }
            _monitors.Add(monitor);
        }

        public void Remove(IContactDetailsMonitor monitor)
        {
            if (!_monitors.Contains(monitor))
            {
                return;
            }
            _monitors.Remove(monitor);
        }

        public void Set(ContactDetails details)
        {
            _latest = details;
            foreach (var monitor in _monitors)
            {
                monitor.Set(details);
            }
        }
    }
}